// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

var targetWindow = null;
var tabCount = 0;

function start(tab) {
  chrome.windows.getCurrent(getWindows);
}

function getWindows(win) {
  targetWindow = win;
  chrome.tabs.getAllInWindow(targetWindow.id, getTabs);
}

function getTabs(tabs) {
  tabCount = tabs.length;
  // We require all the tab information to be populated.
  // chrome.windows.getAll({"populate" : true}, moveTabs);
  var time = new Date().format('yyyy-MM-dd hh:mm:ss');
  var array = new Array();
  for (var i = 0; i < tabCount; i++) {
    var item = {
      title: tabs[i].title,
      url: tabs[i].url
    }
    array[array.length] = item;
  }

  // chrome.storage.local.clear(function () { 
  //   console.log("清除"); 
  // })

  var obj = {
    time:time,
    array:array
  }

  var arr3 = new Array();
  chrome.storage.local.get("tabs", function (data) {
    console.log(data.tabs);
    console.log(data);
    if(data.tabs){            
      console.log('追加');
      arr3 = data.tabs;
      arr3.push(obj);    
    }else{
      arr3.push(obj);     
      console.log('初始');   
    }
    var obj3 = {
      "tabs":arr3
    }
    chrome.storage.local.set(obj3, function () { 
      console.log("保存完毕"); 
    })
  })
}

function moveTabs(windows) {
  var numWindows = windows.length;
  var tabPosition = tabCount;

  for (var i = 0; i < numWindows; i++) {
    var win = windows[i];

    if (targetWindow.id != win.id) {
      var numTabs = win.tabs.length;

      for (var j = 0; j < numTabs; j++) {
        var tab = win.tabs[j];
        // Move the tab into the window that triggered the browser action.
        chrome.tabs.move(tab.id,
          { "windowId": targetWindow.id, "index": tabPosition });
        tabPosition++;
      }
    }
  }
}

// 格式化时间函数
Date.prototype.format = function (format) {
  var o = {
    "M+": this.getMonth() + 1, //month
    "d+": this.getDate(),    //day
    "h+": this.getHours(),   //hour
    "m+": this.getMinutes(), //minute
    "s+": this.getSeconds(), //second
    "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
    "S": this.getMilliseconds() //millisecond
  }
  if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o) if (new RegExp("(" + k + ")").test(format))
    format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
  return format;
}

function checkNotification() {

  if (!("Notification" in window)) {

    alert("This browser does not support desktop notification");

  }

  // check whether notification permissions have alredy been granted

  else if (Notification.permission === "granted") {

    // If it's okay let's create a notification

    new Notification("Granted!");

  }

  // Otherwise, ask the user for permission

  else if (Notification.permission !== 'denied') {

    Notification.requestPermission(function (permission) {

      // If the user accepts, let's create a notification

      if (permission === "granted") {

        new Notification("Request granted!");

      }

    });

  }

}
// // Set up a click handler so that we can merge all the windows.
chrome.browserAction.onClicked.addListener(start);